<style lang="less">
</style>

<template>
  <div>
    <iview-table
            ref="table"
            :data="data"
            :baseURL="queryModel.baseURL"
            :menuId="dataModel.menuId"
            :columns="dataModel.columns"
            :order="dataModel.order"
            :pageNumber="pageModel.pageNumber"
            :pageSize="pageModel.pageSize"
            :defaultFilters="queryModel.defaultFilters"
            v-on="{changeTotalSize:changeTotalSize,changeSelect:changeSelect,getData:getData}">
    </iview-table>
  </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        props: {
            patientId: {
                type: Number,
                default: -1
            },
            data: {
                type: Array,
                default: []
            }
        },
        data () {
            return {
                chargeURL: '/user/charge',
                selectRows: [],
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/charge',
                    defaultFilters: [
                        {'field': 'patient_id', 'value1': -1, 'operate': '=='},
                        {'field': 'charge_status', 'value1': 0, 'operate': '=='}
                    ]
                },
                dataModel: {
                    menuId: -1,
                    columns: [{
                            title: '商品名称',
                            key: 'itemName'
                        },{
                            title: '单价',
                            key: 'price'
                        },{
                            title: '数量',
                            key: 'quantity'
                        },{
                            title: '应付',
                            key: 'totalPrice'
                        },{
                            title: '折扣',
                            key: 'discount',
                            render: (h, params) => {
                                if (params.row.discount === 0) {
                                    return '100%'
                                } else {
                                    return params.row.discount
                                }
                            }
                        },{
                            title: '优惠金额',
                            key: 'discountAmount'
                        },{
                            title: '实付',
                            key: 'actualAmount',
                            render: (h, params) => {
                                var vm = this
                                let value = params.row.actualAmount
                                if (params.row.discountAmount === 0) {
                                    value = params.row.totalPrice
                                    params.row.actualAmount = value
                                    vm.data[params.index] = params.row
                                }
                                return h('InputNumber',{
                                    props: {
                                        min: 0,
                                        value: value
                                    },
                                    style: {
                                        width: '100%'
                                    },
                                    on: {
                                        'on-change' (value) {
                                            params.row.actualAmount = value
                                            vm.data[params.index] = params.row
                                            vm.$emit('compute', vm.data)
                                        }
                                    }
                                });
                            }
                        }
                    ],
                    order: 'creation_time asc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            };
        },
        methods: {
            getData (data) {
                this.$emit('compute', data)
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            refresh () {
                this.$refs.table.refresh()
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            this.init()
        },
        watch: {
            patientId: function (val, oldVal) {
                this.queryModel.defaultFilters[0] = {'field': 'patient_id', 'value1': val, 'operate': '=='}
            }
        }
    }
</script>

<style>

</style>
